from django.shortcuts import render
from django.http import JsonResponse
from datetime import datetime, timedelta
import pytz
from app01 import models
from app01.utils.tool import DateTool, ChartTime, ChartHourTime, CharDepartmentTime
from django.views.decorators.csrf import csrf_exempt


def chart_list_7(request):
    """ 7天数据统计页面 """
    if request.session.get("info")["level"] == 0:
        return render(request, 'u_chart_list_7.html')
    return render(request, 'chart_list_7.html')


def chart_list_30(request):
    """ 30天数据统计页面 """
    if request.session.get("info")["level"] == 0:
        return render(request, 'u_chart_list_30.html')
    return render(request, 'chart_list_30.html')


@csrf_exempt
def chart_bar_7(request):
    """ 构造柱状图的数据 """
    # 数据去数据库中获取

    ct = ChartTime(7)

    dates = ct.get_dates()
    date_counts = ct.get_counts()

    legend = ["人数"]
    series_list = [
        {
            "name": '人数',
            "type": 'bar',
            "data": date_counts
        },
    ]
    x_axis = dates

    result = {
        "status": True,
        "data": {
            'legend': legend,
            'series_list': series_list,
            'x_axis': x_axis,
        }
    }
    return JsonResponse(result)


@csrf_exempt
def chart_line_7(request):
    ch = ChartHourTime(7)

    date_hour = ch.date_hour
    get_counts = ch.get_counts()

    legend = ["人数"]
    series_list = [
        {
            "name": '人数',
            "type": 'line',
            "stack": 'Total',
            "data": get_counts
        },
    ]
    x_axis = date_hour

    result = {
        "status": True,
        "data": {
            'legend': legend,
            'series_list': series_list,
            'x_axis': x_axis,
        }
    }
    return JsonResponse(result)


@csrf_exempt
def chart_pie_7(request):
    """ 构造饼图的数据 """

    cd = CharDepartmentTime(7)
    data = cd.get_departs()
    # print(data)

    db_data_list = data

    result = {
        "status": True,
        "data": db_data_list
    }
    return JsonResponse(result)


@csrf_exempt
def chart_bar_30(request):
    """ 构造柱状图的数据30天 """
    # 数据去数据库中获取

    ct = ChartTime(30)

    dates = ct.get_dates()
    date_counts = ct.get_counts()
    # print(dates)
    # print(date_counts)

    legend = ["人数"]
    series_list = [
        {
            "name": '人数',
            "type": 'bar',
            "data": date_counts
        },
    ]
    x_axis = dates

    result = {
        "status": True,
        "data": {
            'legend': legend,
            'series_list': series_list,
            'x_axis': x_axis,
        }
    }
    return JsonResponse(result)


@csrf_exempt
def chart_line_30(request):
    ch = ChartHourTime(30)

    date_hour = ch.get_hours()
    date_counts = ch.get_counts()

    legend = ["人数"]
    series_list = [
        {
            "name": '人数',
            "type": 'line',
            "stack": 'Total',
            "data": date_counts
        },
    ]
    x_axis = date_hour

    result = {
        "status": True,
        "data": {
            'legend': legend,
            'series_list': series_list,
            'x_axis': x_axis,
        }
    }
    return JsonResponse(result)


@csrf_exempt
def chart_pie_30(request):
    """ 构造饼图的数据 """

    cd = CharDepartmentTime(30)
    data = cd.get_departs()
    # print(data)

    db_data_list = data

    result = {
        "status": True,
        "data": db_data_list
    }
    return JsonResponse(result)
